package com.slt.android.screenshot;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button shot, click;
    Button no;
    Button upload , clear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        shot = (Button) findViewById(R.id.shot);
        click = (Button) findViewById(R.id.click);
        no = (Button) findViewById(R.id.stop);
        upload = (Button) findViewById(R.id.upload);
        clear = (Button) findViewById(R.id.clear);


        shot.setOnClickListener(this);
        click.setOnClickListener(this);
        no.setOnClickListener(this);
        upload.setOnClickListener(this);
        clear.setOnClickListener(this);
        AbSharedUtil.putBoolean(this , "exitFlag" , false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shot:
                handleShot();
                break;
            case R.id.click:
                handleClick();
                break;
            case R.id.stop:
                AbSharedUtil.putBoolean(this , "exitFlag" , true);
                break;
            case R.id.upload:
                handleUpload();
                break;
            case R.id.clear:
                handleClear();
                break;

            default:
                break;
        }
    }

    /**
     * 开启上传任务
     */
    private void handleUpload() {
        Intent i = new Intent(this , UploadService.class);
        startService(i);
    }
    /**
     * 清理截屏文件夹
     */
    private void handleClear() {
        File dir = new File("/sdcard/mysshhoottss/tmp");
        if(dir.exists()){
            deleteAllFiles(dir);
            Toast.makeText(this , "清理完毕" , Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this , "文件夹是空的" , Toast.LENGTH_SHORT).show();
        }
    }

    private void handleClick() {
        Intent i = new Intent(this, ClickService.class);
        startService(i);
    }

    /**
     * 处理shot定时任务
     */
    private void handleShot() {
        Intent i = new Intent(this, ShotService.class);
        startService(i);
    }

    private void deleteAllFiles(File root) {
        File files[] = root.listFiles();
        if (files != null)
            for (File f : files) {
                if (f.isDirectory()) { // 判断是否为文件夹
                    deleteAllFiles(f);
                    try {
                        f.delete();
                    } catch (Exception e) {
                    }
                } else {
                    if (f.exists()) { // 判断是否存在
                        deleteAllFiles(f);
                        try {
                            f.delete();
                        } catch (Exception e) {
                        }
                    }
                }
            }
    }
}
